<%--
  Created by IntelliJ IDEA.
  User: TYZ
  Date: 2020/3/12
  Time: 19:16
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <%@ include file="/common/common.jsp" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <script type="text/javascript">
        var setting = {
        async: {
        enable: true,
        url:"<%=path %>/menuController/getZTree",
        autoParam: ["id", "name"]
        },
        check: {
        enable: true,
        chkStyle: "checkbox",
        chkboxType: {"Y": "p", "N": "s"}
        },view : {
        addHoverDom: function(treeId, treeNode){
        var aObj = $("#" + treeNode.tId + "_a"); // tId = permissionTree_1, ==> $("#permissionTree_1_a")
        aObj.attr("href", "javascript:;");
        aObj.attr("target","_self");
        if (treeNode.editNameFlag || $("#btnGroup"+treeNode.tId).length>0) return;
        var s = '<span id="btnGroup'+treeNode.tId+'">';
        if ( treeNode.level == 0 ) {
        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode('+treeNode.id+')" href="#" >添加</a>';
        } else if ( treeNode.level == 1 ) {
        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode('+treeNode.id+')" href="#" title="修改权限信息">修改</a>';
        if (treeNode.children == undefined) {
        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode('+treeNode.id+')" href="#" >删除</a>';
        }
        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="addNode('+treeNode.id+')" href="#" >添加</a>';
        } else if ( treeNode.level == 2 ) {
        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;"  onclick="editNode('+treeNode.id+')" href="#" title="修改权限信息">修改</a>';
        s += '<a class="btn btn-info dropdown-toggle btn-xs" style="margin-left:10px;padding-top:0px;" onclick="deleteNode('+treeNode.id+')" href="#">删除</a>';
        }
        s += '</span>';
        aObj.after(s);
        },
        removeHoverDom: function(treeId, treeNode){
        $("#btnGroup"+treeNode.tId).remove();
        }
        }
        };

        $(document).ready(function(){
        $.fn.zTree.init($("#menuZtree"), setting);
        });
        //增加
        function addNode(id){
        //传入当前节点的id
        BootstrapDialog.show({
        title: '增加菜单',
        message: $('<div></div>').load('<%=path %>/menuController/toAdd'),
        type: BootstrapDialog.TYPE_SUCCESS,
        size: BootstrapDialog.SIZE_SMALL,
        buttons: [{
        label: '取消',
        cssClass : "btn btn-info",
        action: function (dialog) {
        dialog.close();
        }
        }, {
        label: '确定',
        cssClass : "btn btn-info",
        action: function (dialog) {
        $.ajax({
        url : "<%=path %>/menuController/addMenu",
        type : "post",
        data : {
        text:$("#text").val(),
        selectable:$("#selectable").val(),
        icon:$("#icon").val(),
        url:$("#url").val(),
        selectedIcon:$("#selectedIcon").val(),
        color:$("#color").val(),
        backColor:$("#backColor").val(),
        href:$("#href").val(),
        pid:id
        },
        dataType : "json",
        async : false,
        success : function(result){
        dialog.close();
        toastr.success("成功");
        $.fn.zTree.init($("#menuZtree"), setting);
        },
        error :function(){
        toastr.error("系统异常,请联系管理员");
        }
        })
        }
        }]
        });
        }
        //修改
        function editNode(id){
        BootstrapDialog.show({
        title: '修改菜单',
        message: $('<div></div>').load('<%=path %>/menuController/toUpdate/' + id),
        type: BootstrapDialog.TYPE_SUCCESS,
        size: BootstrapDialog.SIZE_SMALL,
        buttons: [{
        label: '取消',
        cssClass : "btn btn-info",
        action: function (dialog) {
        dialog.close();
        }
        }, {
        label: '确定',
        cssClass : "btn btn-info",
        action: function (dialog) {
        $.ajax({
        url : "<%=path %>/menuController/updateMenu",
        type : "post",
        data : {
        id:id,
        text:$("#text").val(),
        selectable:$("#selectable").val(),
        icon:$("#icon").val(),
        url:$("#url").val(),
        selectedIcon:$("#selectedIcon").val(),
        color:$("#color").val(),
        backColor:$("#backColor").val(),
        href:$("#href").val()
        },
        dataType : "text",
        async : false,
        success : function(data){
        if (data == "1") {
        toastr.success("成功");
        dialog.close();
        $.fn.zTree.init($("#menuZtree"), setting);
        }else {
        toastr.success("修改失败");
        dialog.close();
        }
        },
        error :function(){
        toastr.error("系统异常,请联系管理员");
        }
        })
        }
        }]
        });
        }
        //删除
        function deleteNode(id){
        if (confirm("是否确认删除")){
        $.ajax({
        url : "<%=path %>/menuController/deleteMenu/" + id,
        type : "post",
        dataType : "text",
        async : false,
        success : function(data){
        if (data == "1") {
        toastr.success("成功");
        $.fn.zTree.init($("#menuZtree"), setting);
        } else {
        toastr.error("删除失败");
        }
        },
        error :function(){
        toastr.error("系统异常,请联系管理员");
        }
        })
        }

        }

    </script>
    <ul id="menuZtree" class="ztree"></ul>
</body>
</html>
